﻿using System;
using System.Collections.Generic;

namespace QstkCore.Domain
{
    /// <summary>
    /// Holds a list of dates along with a mapping to an index.
    /// </summary>
    public class TimeSeries
    {
        readonly Dictionary<DateTime, int> dateToOffset = new Dictionary<DateTime, int>();
        private readonly int count;
        public readonly DateTime Start;

        public TimeSeries(IEnumerable<DateTime> timestamps)
        {
            Start = DateTime.MaxValue;
            int dayIndex = 0;
            foreach (DateTime timestamp in timestamps)
            {
                dateToOffset.Add(timestamp.Date, dayIndex++);
                count++;
                if (timestamp < Start)
                    Start = timestamp;
            }
        }

        public int Count
        {
            get { return count; }
        }

        public int GetOffsetDaysFromStart(DateTime date)
        {
            int offset;
            if (!dateToOffset.TryGetValue(date, out offset))
                return -1;
            return offset;
        }
    }
}